using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Markup;
using Rhizome.Contract;
using Rhizome.Contract.Diag;
using Rhizome.Model.Validation;

namespace Rhizome.Model
{
	[ContentProperty("Message")]
	public sealed class SendMessage : RhizomeElement, IAction, IValidate
	{
		private IMessage _message;
		private bool _isMessageSet;

		public IMessage Message
		{
			[DebuggerStepThrough]
			get { return _message; }
			[DebuggerStepThrough]
			set
			{
				_message = value;
				_isMessageSet = true;
			}
		}

	    private IMessageSink MessageSink
		{
			[DebuggerStepThrough]
			get { return GetValue(MessageSyncProperty); }
		}

		#region Implementation of IAction

		public void Process()
		{
			MessageSink.Send(this, Message, new List<IMessageSink>());
		}

		#endregion

		#region Implementation of IValidate

		public IEnumerable<IValidationResult> GetValidationResults()
		{
			if (MessageSink == null) yield return ValidationResult.MessageSinkNotSet;
			if (_isMessageSet == false) yield return ValidationResult.MessageNotSet;
		}

		#endregion

		public override string ToString()
		{
			return string.Format("SendMessage {0}", Message);
		}
	}
}